package cn.wellwan.mybatis.proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * creator: maokai
 * on: 2018/3/7
 * description: cglib动态代理
 */
public class CglibProxy implements MethodInterceptor {

    private Object target;
    /**
     * 创建代理对象
     */
    public Object getInstance(Object target){
        this.target = target;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(this.target.getClass());
        //回调方法
        enhancer.setCallback(this);
        //创建代理对象
        return  enhancer.create();

    }


    @Override
    public Object intercept (Object o , Method method , Object[] objects , MethodProxy methodProxy) throws Throwable {
        Object result = methodProxy.invokeSuper(o , objects);
        return result;
    }
}
